home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / ax25user.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  3.2 KB  |  157 lines

  1. /* User subroutines for AX.25 */
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "iface.h"
  6. #include "ax25.h"
  7. #include "lapb.h"
  8. #include <ctype.h>
  9.  
  10. /* Open an AX.25 connection */
  11. struct ax25_cb *
  12. open_ax25(addr,window,r_upcall,t_upcall,s_upcall,iface,user)
  13. struct ax25 *addr;        /* Addresses */
  14. int16 window;            /* Window size in bytes */
  15. void (*r_upcall)();        /* Receiver upcall handler */
  16. void (*t_upcall)();        /* Transmitter upcall handler */
  17. void (*s_upcall)();        /* State-change upcall handler */
  18. struct interface *iface;    /* Hardware interface structure */
  19. char *user;            /* User linkage area */
  20. {
  21.     struct ax25_cb *axp,*cr_ax25();
  22.     void lapbstate();
  23.  
  24.     if((axp = cr_ax25(&addr->dest)) == NULLAX25)
  25.         return NULLAX25;
  26.     ASSIGN(axp->addr,*addr);
  27.     if(addr->ndigis != 0){
  28.         axp->t1.start *= (addr->ndigis + 1);
  29.         axp->t2.start *= (addr->ndigis + 1);
  30.         axp->t3.start *= (addr->ndigis + 1);
  31.     }
  32.     axp->window = window;
  33.     axp->r_upcall = r_upcall;
  34.     axp->t_upcall = t_upcall;
  35.     axp->s_upcall = s_upcall;
  36.     axp->interface = iface;
  37.     axp->user = user;
  38.  
  39.     switch(axp->state){
  40.     case DISCONNECTED:
  41.         /* Don't send anything if the connection already exists */
  42.         est_link(axp);
  43.         lapbstate(axp,SETUP);
  44.         break;
  45.     case SETUP:
  46.         free_q(&axp->txq);
  47.         break;
  48.     case DISCPENDING:    /* Ignore */
  49.     case FRAMEREJECT:
  50.         break;
  51.     case RECOVERY:
  52.     case CONNECTED:
  53.         free_q(&axp->txq);
  54.         est_link(axp);
  55.         lapbstate(axp,SETUP);
  56.         break;
  57.     }
  58.     return axp;
  59. }
  60.  
  61. /* Send data on an AX.25 connection. Caller must provide PID */
  62. int
  63. send_ax25(axp,bp)
  64. struct ax25_cb *axp;
  65. struct mbuf *bp;
  66. {
  67.     if(axp == NULLAX25 || bp == NULLBUF)
  68.         return -1;
  69.     enqueue(&axp->txq,bp);
  70.     return lapb_output(axp);
  71. }
  72.  
  73. /* Receive incoming data on an AX.25 connection */
  74. struct mbuf *
  75. recv_ax25(axp,cnt)
  76. register struct ax25_cb *axp;
  77. int16 cnt;
  78. {
  79.     struct mbuf *bp;
  80.  
  81.     if(axp->rxq == NULLBUF)
  82.         return NULLBUF;
  83.  
  84.     bp = axp->rxq;
  85.     axp->rxq = NULLBUF;
  86.  
  87.     /* If this has un-busied us, send a RR to reopen the window */
  88.     if(len_mbuf(bp) >= axp->window)
  89.         sendctl(axp,RESPONSE,RR);
  90.     return bp;
  91. }
  92.  
  93. /* Close an AX.25 connection */
  94. disc_ax25(axp)
  95. struct ax25_cb *axp;
  96. {
  97.     void lapbstate();
  98.  
  99.     switch(axp->state){
  100.     case DISCONNECTED:
  101.         break;        /* Ignored */
  102.     case DISCPENDING:
  103.         lapbstate(axp,DISCONNECTED);
  104.         del_ax25(axp);
  105.         break;
  106.     case SETUP:
  107.     case CONNECTED:
  108.     case RECOVERY:
  109.     case FRAMEREJECT:
  110.         free_q(&axp->txq);
  111.         axp->retries = 0;
  112.         sendctl(axp,COMMAND,DISC|PF);
  113.         stop_timer(&axp->t3);
  114.         start_timer(&axp->t1);
  115.         lapbstate(axp,DISCPENDING);
  116.         break;
  117.     }
  118. }
  119.  
  120. /* Verify that axp points to a valid ax25 control block */
  121. ax25val(axp)
  122. struct ax25_cb *axp;
  123. {
  124.     register struct ax25_cb *axp1;
  125.     register int i;
  126.  
  127.     if(axp == NULLAX25)
  128.         return 0;    /* Null pointer can't be valid */
  129.     for(i=0; i < NHASH; i++)
  130.         for(axp1 = ax25_cb[i];axp1 != NULLAX25; axp1 = axp1->next)
  131.             if(axp1 == axp)
  132.                 return 1;
  133.     return 0;
  134. }
  135.  
  136. /* Force a retransmission */
  137. kick_ax25(axp)
  138. struct ax25_cb *axp;
  139. {
  140.     void recover();
  141.  
  142.     if(!ax25val(axp))
  143.         return -1;
  144.     recover((int *)axp);
  145.     return 0;
  146. }
  147.  
  148. /* Abruptly terminate an AX.25 connection */
  149. reset_ax25(axp)
  150. struct ax25_cb *axp;
  151. {
  152.     void lapbstate();
  153.  
  154.     lapbstate(axp,DISCONNECTED);
  155.     del_ax25(axp);
  156. }
  157.